﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace WpfStarterKit.Framework.ViewModel
{
    /// <summary>
    /// Identifies a method that will be called when control returns from another view model
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class ReturnMethodAttribute : Attribute
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="fromNavigationAction">The method is called when control returns from this navigation action</param>
        public ReturnMethodAttribute(string fromNavigationAction)
        {
            FromNavigationAction = fromNavigationAction;
        }

        /// <summary>
        /// The method is called when control returns from this navigation action
        /// </summary>
        public string FromNavigationAction { get; set; }

        /// <summary>
        /// Returns the ReturnMethodAttribute applied (if one was applied) to the method
        /// </summary>
        /// <param name="mInfo">Method info</param>
        /// <returns>ReturnMethodAttribute if one was applied to the method, null if it isn't</returns>
        public static ReturnMethodAttribute GetAttribute(MethodInfo mInfo)
        {
            var returnMethodAttributes = mInfo.GetCustomAttributes(typeof(ReturnMethodAttribute), false);
            if (returnMethodAttributes != null && returnMethodAttributes.Count() > 0)
            {
                return returnMethodAttributes[0] as ReturnMethodAttribute;
            }

            return null;
        }
    }
}
